package com.zcp.basetemplatedemo;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.SparseArray;
import android.view.View;

/**
 * Created by Administrator on 2017/3/30.
 */

public abstract class BaseActivity extends AppCompatActivity implements View.OnClickListener {

    private SparseArray<View> mViews;

    public abstract int getLayoutId();//相对应的布局

    public abstract void initViews();//相当于findViewById

    public abstract void initListener();//监听

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayoutId());
        mViews = new SparseArray<>();
        initViews();
        initListener();

    }

    //通过Id找到view
    public <E extends View> E findView(int viewId) {
        E view = (E) mViews.get(viewId);
        if (view == null) {
            view = (E) findViewById(viewId);
            mViews.put(viewId, view);
        }
        return view;
    }

    //View设置onClick事件
    public <E extends View> void setOnClick(E view) {
        view.setOnClickListener(this);
    }
}
